import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/assessments.dart';
import 'package:ichinesereader/models/assessment/assessmentStudentQueryDto.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/assessmentsDataGrid.dart';

import 'reportsStudentPersonalContent.dart';

class ReportStudentPersonalAssessment extends StatefulWidget {
  ReportStudentPersonalAssessment({
    Key? key,
    required this.classId,
    required this.userName,
  }) : super(key: key);
  final String classId;
  final String userName;

  @override
  ReportStudentPersonalAssessmentState createState() =>
      ReportStudentPersonalAssessmentState();
}

class ReportStudentPersonalAssessmentState
    extends State<ReportStudentPersonalAssessment> {
  final AssessmentsController _assessmentsController = Get.find();
  var box = Hive.box('appconfig');
  List<String> _status = [];

  @override
  void initState() {
    studentPersonalReportTab = Constants.studentPersonalReportTabs[5];
    if (_assessmentsController.studentAssessmentRecord.value.list != null &&
        _assessmentsController.studentAssessmentRecord.value.list!.isNotEmpty) {
      _assessmentsController.studentAssessmentRecord.value.list!.clear();
    }
    fetchOverallData(widget.classId, widget.userName, _status);
    super.initState();
  }

  void fetchOverallData(String classId, String userName, List<String> status) {
    AssessmentStudentQueryDto dto = new AssessmentStudentQueryDto(
        status: status, type: [], userName: userName);
    Future.delayed(Duration(milliseconds: 100), () {
      _assessmentsController.fetchStudentAssessmentRecords(classId, dto);
    });
  }

  void resetStatus() {
    this.setState(() {
      _status = [];
    });
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build SchoolAdmin reportsStudentPersonal assessment');
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    return Center(
        child: Padding(
      padding: const EdgeInsets.all(10),
      child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: Constants.statusFilters.map((op) {
            return Row(children: [
              Checkbox(
                  value: _status.contains(op.value),
                  onChanged: (bool? value) {
                    List<String> temp = _status;
                    if (_status.contains(op.value)) {
                      temp.removeWhere((str) {
                        return str == op.value;
                      });
                    } else {
                      temp.add(op.value);
                    }
                    this.setState(() {
                      _status = temp;
                    });
                    fetchOverallData(widget.classId, widget.userName, temp);
                  }),
              Text(op.label!),
              SizedBox(
                width: 12,
              )
            ]);
          }).toList(),
        ),
        SizedBox(
          height: 10,
        ),
        Expanded(
          child: _assessmentsController.obx((data) {
            if (_assessmentsController.studentAssessmentRecord.value.list !=
                    null &&
                _assessmentsController
                    .studentAssessmentRecord.value.list!.isNotEmpty) {
              return Container(
                  //height: 300,
                  child: AssessmentsDataGrids(
                      isTeacher,
                      _assessmentsController
                          .studentAssessmentRecord.value.list!,
                      false));
            } else {
              return Center(child: Text('Data not available ...'.tr));
            }
          }),
        ),
      ]),
    ));
  }
}
